import {useState,useEffect} from "react"
function useRequest(fn){
   //fn 是请求函数;
   const [loading,setloading] = useState(false);
   const [data,setData] = useState([]);
   const [error,setError] = useState(null);
   useEffect(async()=>{
      setloading(true);
      try {
        let res = await fn();
        setData(res.data);
        setloading(false);
      } catch (error) {
        setError(error);
        setloading(false);
      }
   },[])
   return {
     loading,
     data,
     error
   }
}

export default useRequest